//
//  NWebViewController.m
//  News
//
//  Created by mac on 2018/11/30.
//  Copyright © 2018 zt.td. All rights reserved.
//

#import "NWebViewController.h"

@interface NWebViewController ()<WKUIDelegate,WKNavigationDelegate,UIWebViewDelegate>

@end

@implementation NWebViewController

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self setHidesBottomBarWhenPushed:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(navHeight);
        make.left.bottom.right.equalTo(self.view);
    }];
    [self loadHTML];
}

- (void)loadHTML
{
    if (self.localHtml) {
        NSString *webViewSoruce = @"";
        NSLog(@"%@",self.localHtml);
        webViewSoruce  = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapes(NULL, (__bridge CFStringRef)self.localHtml, CFSTR(""));
        
        [self.webView loadHTMLString:webViewSoruce baseURL:nil];
    } else {
        NSLog(@"%@",self.netHtml);
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.netHtml]];
        [self.webView loadRequest:request];
    }
    
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    //    if (self.localHtml) {
    //         [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '200%'" completionHandler:nil];
    //    }else{
    //         [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '150%'" completionHandler:nil];
    //    }
}

- (void)webViewDidFinishLoad:(UIWebView*) webView
{
    //获取页面高度，并重置webview的frame
    //    CGFloat documentHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"content\").offsetHeight;"] floatValue];
    //    CGRect frame = webView.frame;
    //    frame.size.height = documentHeight;
    //    webView.frame = frame;
}

- (WKWebView *)webView
{
    if (!_webView) {
        _webView = [[WKWebView alloc]init];
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
        // 这行代码可以是侧滑返回webView的上一级，而不是根控制器（*只针对侧滑有效）
        [_webView setAllowsBackForwardNavigationGestures:true];
    }
    return _webView;
}

@end
